package com.example.usefulutils.util

import android.content.ContentValues

/**
 * vararg 可变参数列表
 * 使用这个方法，可以直接通过 A to B 构建一个 ContentValues
 * 例如 val value = cvOf("name" to "Game of Thrones" , "author“ to "George Martin" , ......)
 *
 * KTX库中提供了一个具有同样功能的ContentValuesOf()函数
 */
fun cvOf(vararg pairs:Pair<String,Any?>) = ContentValues().apply{
    for (pair in pairs){
        val key = pair.first
        when(val value = pair.second){
            is Int ->put(key,value)
            is Long ->put(key,value)
            is Double ->put(key,value)
            is Short ->put(key,value)
            is Float ->put(key,value)
            is Boolean ->put(key,value)
            is String ->put(key,value)
            is Byte ->put(key,value)
            is ByteArray ->put(key,value)
            null -> putNull(key)
        }
    }
}